﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;

namespace PerceptiveMCAPI.Methods
{
   public class generateText : Api_BaseExecute
   {
      private new generateTextInput Input { get; set; }
      private generateTextOutput Output { get; set; }
      // =============================================== constructors
      public generateText() { }
      public generateText( generateTextInput input ) { this.Input = input; }

      #region =============================================== Mainline Execute

      public generateTextOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public generateTextOutput Execute( generateTextInput input )
      {
         Input = input;
         Output = new generateTextOutput( Input );
         ValidationIsOK = true;
         //------------------------- validate it
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #endregion

      #region ================================================================= API Calls

      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            string rpcResult = "";
            string typeValue = Input.parms.type.ToString();
            //
            if ( Input.parms.type == EnumValues.generateText_type.template )
            {
               XmlRpcStruct content = apiXmlRpc.FormatDictionaryToXmlRpcStruct( (Dictionary<string, string>)Input.parms.content );
               rpcResult = apiXmlRpc.api.generateText( Input.parms.apikey, typeValue, content );
            }
            else
            {
               string content = Input.parms.content.ToString();
               rpcResult = apiXmlRpc.api.generateText( Input.parms.apikey, typeValue, content );
            }
            // ----------------------------------------------------------------
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "type", Input.parms.type.ToString() );
            if ( Input.parms.type == EnumValues.generateText_type.template )
            {
               Dictionary<string, string> genDict = (Dictionary<string, string>)Input.parms.content;
               foreach ( KeyValuePair<string, string> kv in genDict )
               {
                  dict.Add( string.Format( "content[{0}]", kv.Key ), kv.Value );
               }
            }
            else
            {
               dict.Add( "content", Input.parms.content.ToString() );
            }
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion
   }
}
